package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.synonym.SolrSynonymParser;
import org.apache.lucene.analysis.synonym.SynonymFilter;
import org.apache.lucene.analysis.synonym.SynonymMap;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.solr.common.luke.FieldFlag;

import com.dhgate.spu.analysis.consts.SysConsts;

public class CopyOfSynoymMapDemo {

	public static void main(String[] args) throws IOException, ParseException {

		

		boolean dedup = true;
		boolean expand = true;
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		SolrSynonymParser parser = new SolrSynonymParser(dedup, expand, analyzer);

		parser.add(new FileReader(new File(SysConsts.CONST_SYNOYM_FILE_PATH)));
		SynonymMap build = parser.build();
		
//		StringReader reader = new StringReader("MT6573,MTK6276M");
		String title = "2014 New Designer Ball Gown Sweetheart Beading Off The hShoulder Sleeveless Floor-Length Organza Evening Dresses";
		StringReader reader = new StringReader(title);
		TokenStream ts = analyzer.tokenStream("", reader);

		SynonymFilter synonymFilter = new SynonymFilter(ts, build, true);
		CharTermAttribute term = synonymFilter.getAttribute(CharTermAttribute.class);
		synonymFilter.reset();
		String attr = "Floor Length";
		String a[] = attr.split(" ");
		StringBuilder rString = new StringBuilder();
		while (synonymFilter.incrementToken()) {
			if(term.toString().equals(attr.toLowerCase())){
				System.out.println(term.toString());
			}
			rString.append(term.toString()+" ");
		}
		if((a.length > 1 && rString.indexOf(attr.toLowerCase()) != -1) || title.toLowerCase().indexOf(attr.toLowerCase()) != -1){
			System.out.println(attr);
		}
		System.out.println(rString);
		
		
		
		ParseSynoym pSynoym = new SimpleParseSynoym();
		pSynoym.loadSynoym(new File(SysConsts.CONST_SYNOYM_FILE_PATH));
		String parseTitle = pSynoym.parseString(title);
		System.out.println(parseTitle);
		if((a.length > 1 && rString.indexOf(attr.toLowerCase()) != -1) || title.toLowerCase().indexOf(attr.toLowerCase()) != -1){
			System.out.println(attr);
		}		
		System.out.println();

	
	
	
	}	
}
